﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UE.SharePoint.ImportExport.BLL.SharePoint;
using UE.SharePoint.ImportExport.BLL.Excel;
using Microsoft.SharePoint;

namespace UE.SharePoint.ImportExport.BLL
{
    /// <summary>
    /// Represents a manager how import extra file to sharepoint list.
    /// </summary>
    public class ImportManager
    { 
        private IReader _sourceReader = null;
        private IWritter _targetWriter = null;
        private IReader _targetReader = null;

        /// <summary>
        /// Executes to import 
        /// </summary>
        /// <param name="spSite"></param>
        /// <param name="spWeb"></param>
        /// <param name="spListId"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool Execute(SPSite spSite, SPWeb spWeb, string spListId, string spFolder, string fileName, IStatus status, bool isSPFile = true)
        {
            bool result = false;
            Setting setting = new Setting();
            setting.SPSite = spSite;
            setting.SPWeb = spWeb;
            setting.IsSPFile = isSPFile;
            setting.SPFolderUrl = spFolder;
            setting.FileName = fileName;
            setting.SPList = setting.SPWeb.Lists[new Guid(spListId)];

            InitProvider(setting,status);
            result = _targetWriter.Writer(_sourceReader.GetData()); 

            return result;
        }

        /// <summary>
        /// Inits provider
        /// </summary>
        /// <param name="setting"></param>
        private void InitProvider(Setting setting,IStatus status)
        {
            //this is repeated by ioc.
            _targetReader = new SPListReader(setting);            

            switch (setting.FileExt)
            {
                case "xls":
                    {
                        _sourceReader = new XLSReader(setting);
                        break;
                    }
                case "xlsx":
                    {
                        _sourceReader = new XLSXReader(setting);
                        break;
                    }
                default:
                    //error
                    break;
            }

            _targetWriter = new SPListWriter(setting, _targetReader, status); 
        }

        /// <summary>
        /// Executes to import 
        /// </summary>
        /// <param name="spSiteUrl"></param>
        /// <param name="spWebUrl"></param>
        /// <param name="spListId"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool Execute(string spSiteUrl, string spWebUrl, string spListId, IStatus status, string fileName = null)
        {
            SPSite spSite = new SPSite(spSiteUrl);

            SPWeb spWeb = string.IsNullOrEmpty(spWebUrl) ? spSite.OpenWeb() : spSite.OpenWeb(spWebUrl);

            return Execute(spSite, spWeb, spListId, "", fileName, status, false);
        }
    }
}
